package com.mashang.mapper;

import com.mashang.common.PageVo;
import com.mashang.domain.Student;
import com.mashang.util.JdbcUtil;
import com.mashang.util.StrUtil;
import com.mashang.vo.student.StudentQueryVo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class StudentMapper {

    public List<Student> selectStudentPageList(Student student, PageVo pageVo) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        List<Student> studentList = new ArrayList<>();
        try {
            connection = JdbcUtil.getConnection();
            statement = connection.createStatement();

            // 封装sql
            String sql = "select * from student where 1=1 ";
            String condition = condition(student);
            sql = sql + condition;

            int beginIndex = (pageVo.getPageNum() - 1) * pageVo.getPageSize();
            sql = sql + " limit " + beginIndex + "," + pageVo.getPageSize();

            //执行sql
            resultSet = statement.executeQuery(sql);

            //解析返回的结果
            while (resultSet.next()) {
                Student stu = new Student();
                stu.setStudentId(resultSet.getLong("student_id"));
                stu.setName(resultSet.getString("name"));
                stu.setAge(resultSet.getInt("age"));
                stu.setSex(resultSet.getString("sex"));
                stu.setPhone(resultSet.getString("phone"));
                stu.setCreateTime(resultSet.getString("create_time"));
                stu.setClassId(resultSet.getLong("class_id"));

                studentList.add(stu);
            }

            return studentList;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(connection, statement, resultSet);
            return studentList;
        }

    }


    public Long count(Student student, PageVo pageVo) {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        Long result = 0l;
        try {
            connection = JdbcUtil.getConnection();
            statement = connection.createStatement();

            // 封装sql
            String sql = "select count(*) from student";
            String condition = condition(student);
            sql = sql + condition;

            //执行sql
            resultSet = statement.executeQuery(sql);

            //解析返回的结果
            while (resultSet.next()) {
                result = resultSet.getLong(1);
            }

            return result;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(connection, statement, resultSet);
            return result;
        }

    }

    private String condition(Student student) {
        String sql = "";
        if (student.getStudentId() != null) {
            sql = sql + " and student_id=" + student.getStudentId();
        }
        if (StrUtil.isNotEmpty(student.getName())) {
            sql = sql + " and name like '%" + student.getName() + "%'";
        }

        if (StrUtil.isNotEmpty(student.getStudentNo())) {
            sql = sql + " and student_no like '%" + student.getName() + "%'";
        }

        return sql;
    }
}
